package ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;
import javax.swing.UIManager;
import java.awt.Font;

public class PasswordField extends JPanel implements ActionListener, FocusListener

{

//	private AbstractAction tabAction = new AbstractAction() {
//
//		public void actionPerformed(ActionEvent e) {
//			if(border == 1) {
//				usernameField.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
//				passwordField.setBorder(new LineBorder(new Color(153, 180, 209), 2, true));
//				border=0;
//			}
//			else{
//				usernameField.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
//				passwordField.setBorder(new LineBorder(new Color(153, 180, 209), 2, true));
//				border =1;
//			}
//		}
//
//	};

	private AbstractAction doClickAction = new AbstractAction() {

		public void actionPerformed(ActionEvent e) {
			ok.doClick();
		}
	};
    private final Color colour = UIManager.getColor("InternalFrame.activeTitleBackground");
	private static String OK = "Ok";
	private static String CANCEL = "Cancel";

	private JDialog passwordDialog;
	private JTextField usernameField;
	private JPasswordField passwordField;
	private JButton ok;
	private JButton cancel;
	private int border=1;
	public PasswordField() {


		setBackground(UIManager.getColor("Button.highlight"));
		passwordDialog = new JDialog();
		passwordDialog.setAlwaysOnTop(true);
		passwordDialog.setContentPane(this);
		passwordDialog.setSize(360, 211);
		passwordDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(null);

		JLabel username = new JLabel(" Username");
		username.setFont(new Font("Tahoma", Font.BOLD, 11));
		username.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		username.setOpaque(true);
		username.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
		username.setBounds(33, 20, 89, 33);
		add(username);

		JLabel password = new JLabel(" Password");
		password.setFont(new Font("Tahoma", Font.BOLD, 11));
		password.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		password.setOpaque(true);
		password.setBackground(UIManager.getColor("InternalFrame.activeTitleBackground"));
		password.setBounds(31, 72, 93, 33);
		add(password);

		usernameField = new JTextField();
		usernameField.setFont(new Font("Arial", Font.BOLD, 11));
		usernameField.setBorder(new LineBorder(new Color(153, 180, 209), 2, true));
		usernameField.setBounds(148, 18, 184, 32);
		add(usernameField);

		usernameField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setFont(new Font("Arial", Font.BOLD, 11));

		passwordField.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		passwordField.setBounds(148, 74, 183, 30);
		add(passwordField);

		ok = new JButton("Ok");
		ok.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		ok.addActionListener(this);
		ok.setBounds(81, 131, 65, 23);
		add(ok);

		cancel = new JButton("Cancel");
		cancel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		cancel.addActionListener(this);
		cancel.setBounds(189, 130, 81, 23);
		add(cancel);

		usernameField.addFocusListener(this);
		passwordField.addFocusListener(this);
		
//		usernameField.setFocusTraversalKeysEnabled(true);
//		passwordField.setFocusTraversalKeysEnabled(true);
//		
		passwordField.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "clickOk");
//		passwordField.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke("TAB"), "tabAction");
		passwordField.getActionMap().put("clickOK", doClickAction);
//		passwordField.getActionMap().put("tabAction", tabAction);
//		usernameField.getInputMap(WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0), "tabAction");
//		usernameField.getActionMap().put("tabAction", tabAction);

	}

	@Override
	public void setVisible(boolean value) {
		super.setVisible(value);
		passwordDialog.setVisible(value);
		passwordDialog.requestFocus();
		//usernameField.requestFocus();
		passwordDialog.setLocation(100, 100);

		if (value) {
			passwordDialog.toFront();
		}
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if(OK.equals(arg0.getActionCommand())) {
			String username = usernameField.getText();
			char[] password = passwordField.getPassword();
			if(username!=null && password!=null) {
			firePropertyChange("usernamePassword", null, username + " " + new String(password));
			password = null;
			passwordDialog.dispose();
			}
		} else if (CANCEL.equals(arg0.getActionCommand())) {
			passwordDialog.dispose();			
		}
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		((JComponent) arg0.getComponent()).setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		((JComponent) arg0.getComponent()).setBorder(new LineBorder(new Color(153, 180, 209), 2, true));		
	}
}
